Menu Sidebar
Menu

binary tree

Find Next Higher Key in BST (Given Parent)

找后继节点, 但是这里给了父节点. 很简单的考虑一下几种情况: 引用一下http://www.algoqueue.com/的图 比如3, 有右节点, 所以我们返回右节点的最左节点(后继).. 比如2, 没有右节点, 但是2是父节点3的左节点, 所以返回3. 假设没有7, 6没有右节点(没有7), 但是6是3(父节点)的右节点,所以我们只能往上找, 直到找到3是8的左节点, 返回8. package Learning;/** import java.util.*; public class NextHigherKeyinBST { static class TreeNode { // Node 有 parent TreeNode parent; TreeNode left; TreeNode right; int val; private TreeNode(){} public TreeNode(TreeNode p, int val) { this.parent = p; this.val = val; } […]

[LintCode] Subtree

public boolean isSubtree(TreeNode T1, TreeNode T2) { // write your code here if(T2 == null) return true; else if(T1 == null) return false; else return isSameTree(T1, T2) || isSubtree(T1.left, T2) || isSubtree(T1.right, T2); } public boolean isSameTree(TreeNode T1, TreeNode T2) { if(T1 == null && T2 == null) return true; if(T1 == null || T2 […]

[LintCode] Invert Binary Tree

public void invertBinaryTree(TreeNode root) { // write your code here if(root == null) return; invertBinaryTree(root.left); invertBinaryTree(root.right); TreeNode left = root.left; root.left = root.right; root.right = left; }

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

February 2023
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728