樹算法的套路框架
二叉樹算法
:設計的總路線:明確一個節點要做的事情,然後剩下的事拋給框架
void traverse(TreeNode root){
//root 需要做什麼?在這做
//其他的不需要root操心,拋給框架
traverse(root.left);
traverse(root.right);
}
Demon01:如何把二叉樹所有節點值加1
void plusOne(TreeNode root) {
if (root == null) return;
root.val += 1;
plusOne(root.left);
plusOne(root.right);
}
Demon02:如何判斷兩顆二叉樹是否完全相同?
boolean isSameTree(TreeNode root1, TreeNode root2) {
// 都爲空的話,顯然相同
if (root1 == null && root2 == null) return true;
// 一個爲空,一個非空,顯然不同
if (root1 == null || root2 == null) return false;
// 兩個都非空,但 val 不一樣也不行
if (root1.val != root2.val) return false;
// root1 和 root2 該比的都比完了
return isSameTree(root1.left, root2.left)
&& isSameTree(root1.right, root2.right);
}
二叉樹搜索樹
二叉搜索樹(Binary Search Tree,簡稱 BST)
是一種很常用的的二叉樹。它的定義是:一個二叉樹中,任意節點的值要大於等於左子樹所有節點的值,且要小於等於右邊子樹的所有節點的值。
下面實現 BST 的基礎操作:判斷 BST 的合法性、增、刪、查。其中“刪”和“判斷合法性”略微複雜。
判斷是否爲合法二叉樹
boolean isValidBST(TreeNode root) {
if (root == null) return true;
if (root.left != null && root.val <= root.left.val) return false;
if (root.right != null && root.val >= root.right.val) return false;
return isValidBST(root.left)
&& isValidBST(root.right);
}
但是這個算法出現了錯誤,BST 的每個節點應該要小於右邊子樹的所有節點,下面這個二叉樹顯然不是 BST,但是我們的算法會把它判定爲 BST。
出現錯誤,不要慌張,框架沒有錯,一定是某個細節問題沒注意到。我們重新看一下BST
的定義,root
需要做的不只是和左右子節點
比較,而是要整個左子樹和右子樹所有節點比較。怎麼辦,鞭長莫及啊!
這種情況,我們可以使用輔助函數,增加函數參數列表,在參數中攜帶額外信息,請看正確的代碼:
boolean isValidBST(TreeNode root) {
return isValidBST(root, null, null);
}
boolean isValidBST(TreeNode root, TreeNode min, TreeNode max) {
if (root == null) return true;
//當前樹比左子樹的最小值小則返回false
if (min != null && root.val <= min.val) return false;
//當前樹比右子樹的最大值大則返回false
if (max != null && root.val >= max.val) return false;
//判斷左子樹是否爲BST
return isValidBST(root.left, min, root)
&& isValidBST(root.right, root, max);
}
在BST中查找一個數是否存在
boolean isInBST(TreeNode root, int target) {
if (root == null) return false;
if (root.val == target) return true;
return isInBST(root.left, target)
|| isInBST(root.right, target);
}
在你可以考慮一點細節問題了:如何充分利用信息,把 BST 這個“左小右大”的特性用上?
很簡單,其實不需要遞歸地搜索兩邊,類似二分查找思想,根據 target 和 root.val 的大小比較,就能排除一邊。我們把上面的思路稍稍改動:
boolean isInBST(TreeNode root, int target) {
if (root == null) return false;
if (root.val == target)
return true;
if (root.val < target)
return isInBST(root.right, target);
if (root.val > target)
return isInBST(root.left, target);
// root 該做的事做完了,順帶把框架也完成了,妙
}
於是,我們對原始框架進行改造,抽象出一套針對 BST 的遍歷框架:
void BST(TreeNode root, int target) {
if (root.val == target)
// 找到目標,做點什麼
if (root.val < target)
BST(root.right, target);
if (root.val > target)
BST(root.left, target);
}
在BST中插入一個數
對數據結構的操作無非遍歷 + 訪問,遍歷就是“找”
,訪問就是“改”
。具體到這個問題,插入一個數,就是先找到插入位置,然後進行插入操作。
上一個問題,我們總結了 BST 中的遍歷框架,就是“找”的問題。直接套框架,加上“改”的操作即可。一旦涉及“改”,函數就要返回 TreeNode 類型,並且對遞歸調用的返回值進行接收。
TreeNode insertIntoBST(TreeNode root, int val) {
// 找到空位置插入新節點
if (root == null) return new TreeNode(val);
// if (root.val == val)
// BST 中一般不會插入已存在元素
if (root.val < val)
root.right = insertIntoBST(root.right, val);
if (root.val > val)
root.left = insertIntoBST(root.left, val);
return root;
}
在BST中刪除一個數
這個問題稍微複雜,不過你有框架指導,難不住你。跟插入操作類似,先“找”再“改”,先把框架寫出來再說:
TreeNode deleteNode(TreeNode root, int key) {
if (root.val == key) {
// 找到啦,進行刪除
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
}
return root;
}
if (root.left == null && root.right == null)
return null;
// 排除了情況 1 之後
if (root.left == null) return root.right;
if (root.right == null) return root.left;
if (root.left != null && root.right != null) {
// 找到右子樹的最小節點
TreeNode minNode = getMin(root.right);
// 把 root 改成 minNode
root.val = minNode.val;
// 轉而去刪除 minNode
root.right = deleteNode(root.right, minNode.val);
}
TreeNode getMin(TreeNode node) {
// BST 最左邊的就是最小的
while (node.left != null) node = node.left;
return node;
}
三種情況分析完畢,填入框架,簡化一下代碼:
TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (root.val == key) {
// 這兩個 if 把情況 1 和 2 都正確處理了
if (root.left == null) return root.right;
if (root.right == null) return root.left;
// 處理情況 3
TreeNode minNode = getMin(root.right);
root.val = minNode.val;
root.right = deleteNode(root.right, minNode.val);
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else if (root.val < key) {
root.right = deleteNode(root.right, key);
}
return root;
}
TreeNode getMin(TreeNode node) {
// BST 最左邊的就是最小的
while (node.left != null) node = node.left;
return node;
}