LeetCode OJ Convert Sorted List to Binary Search Tree

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;
		}
	}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值