1. 程式人生 > >[LeetCode] 285. Inorder Successor in BST 二叉搜索樹中的中序後繼節點

[LeetCode] 285. Inorder Successor in BST 二叉搜索樹中的中序後繼節點

ive earch fin 二叉樹 urn htm sso == sea

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.

給一個二叉搜索樹和它的一個節點,找出它的中序後繼節點,如果沒有返回null。

解法1: 用中序遍歷二叉搜索樹,當找到root.val = p.val的時候,返回下一個節點。T: O(n), S: O(n)

解法2: 利用BST的性質,不用遍歷全部元素。比較root.val和p.val,然後在左子樹或者右子樹中查找。T: O(h), S: O(1)

如果節點p有右子樹,那麽p的後繼節點為右子樹的的最左值(即最後一個沒有左子樹的節點);

如果節點p沒有右子樹,就在二叉搜索樹中搜索節點p,並在從上往下的查找過程中依次更新記錄比p的val值大的節點。

Java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if(root == null || p == null) {
            return null;
        }
        boolean foundNodeP = false;
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.isEmpty()) {
            if(root != null) {
                stack.push(root);
                root = root.left;
            } else {
                root = stack.pop();
                if(foundNodeP) {
                    return root;
                }
                if(root.val == p.val) {
                    foundNodeP = true;
                }
                root = root.right;
            }
        }
        
        return null;
    }
}

Java:

public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if(root == null || p == null) {
            return null;
        }
        TreeNode successor = null;
        while(root != null) {
            if(p.val < root.val) {
                successor = root;
                root = root.left;
            } else {
                root = root.right;
            }
        }
        
        return successor;
    }
}

Java:

public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode node = root, successor = null;
        while (node != null) {
            if (node.val > p.val) {
                successor = node;
                node = node.left;
            } else {
                node = node.right;
            }
        }
        return successor;
    }
}

Python:

class Solution(object):
    def inorderSuccessor(self, root, p):
        """
        :type root: TreeNode
        :type p: TreeNode
        :rtype: TreeNode
        """
        # If it has right subtree.
        if p and p.right:
            p = p.right
            while p.left:
                p = p.left
            return p

        # Search from root.
        successor = None
        while root and root != p:
            if root.val > p.val:
                successor = root
                root = root.left
            else:
                root = root.right

        return successor

C++:

class Solution {
public:
    TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
        TreeNode *res = NULL;
        while (root) {
            if (root->val > p->val) {
                res = root;
                root = root->left;
            } else root = root->right;
        }
        return res;
    }
};

C++: Recursion

class Solution {
public:
    TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
        if (!root) return NULL;
        if (root->val <= p->val) {
            return inorderSuccessor(root->right, p);
        } else {
            TreeNode *left = inorderSuccessor(root->left, p);
            return left ? left : root;
        }
    }
};

  

類似題目:

[LeetCode] 94. Binary Tree Inorder Traversal 二叉樹的中序遍歷

[LeetCode] 173. Binary Search Tree Iterator 二叉搜索樹叠代器

  

[LeetCode] 285. Inorder Successor in BST 二叉搜索樹中的中序後繼節點