码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode OJ Convert Sorted List to Binary Search Tree

时间:2015-03-31 18:09:23      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:leetcode   algorithm   

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

之前在Sicily做过,就是实现一个AVL树了。

struct TreeNodeNew {
    int val;
    TreeNodeNew *left;
    TreeNodeNew *right;
	int bf;
	TreeNodeNew(int val = 0, TreeNodeNew * left = NULL, TreeNodeNew * right = NULL, int bf = 0) {
		this->val = val;
		this->left = left;
		this->right = right;
		this->bf = bf;
	}
};

class Solution {
public:
	TreeNode * sortedListToBST(ListNode * head) {
		TreeNodeNew * ansNew = NULL;
		ListNode * temp = head;
		while (temp) {
			avlInsert(ansNew, temp->val);
			temp = temp->next;
		}
		TreeNode * ans = NULL;
		if (ansNew) reBuild(ansNew, ans);
		return ans;
	}

	void reBuild(TreeNodeNew * &nowNew, TreeNode * &now) {
		now = new TreeNode(nowNew->val);
		if (nowNew->left) reBuild(nowNew->left, now->left);
		if (nowNew->right) reBuild(nowNew->right, now->right);
	}

	inline int MAX(int a, int b) {
		if (a > b) return a + 1;
		return b + 1;
	}

	void avlInsert(TreeNodeNew * &root, const int entry) {
		bool unbalanced;
		avlInsertInside(root, entry, unbalanced);
	}

	void lR(TreeNodeNew * &parent) {
		TreeNodeNew * child = parent->left;
		if (child->bf == 1) {
			parent->left = child->right;
			child->right = parent;
			parent->bf = 0;
			parent = child;
		}
		else {
			TreeNodeNew * grandChild = child->right;
			parent->left = grandChild->right;
			child->right = grandChild->left;
			grandChild->right = parent;
			grandChild->left = child;
			switch (grandChild->bf) {
			case 0:
				parent->bf = child->bf = 0;
				break;
			case 1:
				parent->bf = -1;
				child->bf = 0;
				break;
			case -1:
				parent->bf = 0;
				child->bf = 1;
			}
			parent = grandChild;
		}
		parent->bf = 0;
	}

	void rR(TreeNodeNew * &parent) {
		TreeNodeNew * child = parent->right;
		if (child->bf == -1) {
			parent->right = child->left;
			child->left = parent;
			parent->bf = 0;
			parent = child;
		}
		else {
			TreeNodeNew * grandChild = child->left;
			parent->right = grandChild->left;
			child->left = grandChild->right;
			grandChild->right = child;
			grandChild->left = parent;
			switch (grandChild->bf) {
			case 0:
				parent->bf = child->bf = 0;
				break;
			case -1:
				parent->bf = 1;
				child->bf = 0;
				break;
			case 1:
				parent->bf = 0;
				child->bf = -1;
			}
			parent = grandChild;
		}
		parent->bf = 0;
	}

	void avlInsertInside(TreeNodeNew * &parent, const int x, bool & unbalanced) {
		if (parent == NULL) {
			parent = new TreeNodeNew(x, 0, 0, 0);
			unbalanced = true;
		}
		else if (x < parent->val) {
			avlInsertInside(parent->left, x, unbalanced);
			if (unbalanced) {
				switch (parent->bf) {
				case 0:
					parent->bf = 1;
					break;
				case -1:
					parent->bf = 0;
					unbalanced = false;
					break;
				case 1:
					lR(parent);
					unbalanced = false;
				}
			}
		}
		else if (x > parent->val) {
			avlInsertInside(parent->right, x, unbalanced);
			if (unbalanced) {
				switch (parent->bf) {
				case 0:
					parent->bf = -1;
					break;
				case 1:
					parent->bf = 0;
					unbalanced = false;
					break;
				case -1:
					rR(parent);
					unbalanced = false;
				}
			}
		}
		else {
			unbalanced = false;
		}
	}
};

LeetCode OJ Convert Sorted List to Binary Search Tree

标签:leetcode   algorithm   

原文地址:http://blog.csdn.net/u012925008/article/details/44781827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!