給定二叉樹根結點root
,此外樹的每個結點的值要麼是 0,要麼是 1。
返回移除了所有不包含 1 的子樹的原二叉樹。
( 節點 x 的子樹為 x 本身,以及所有 x 的後代。)
示例1:輸入:[1,null,0,0,1]輸出:[1,null,0,null,1]解釋:只有紅色節點滿足條件「所有不包含 1 的子樹」。右圖為返回的答案。
示例2:輸入:[1,0,1,0,0,0,1]輸出:[1,null,1,null,1]
示例3:輸入:[1,1,0,1,1,0,1,0]輸出:[1,1,0,1,1,null,1]說明:
package solution814;
class solution
root.left = prunetree(root.left);
root.right = prunetree(root.right);
if (root.left == null && root.right == null && root.val == 0)
return root;
} static void inorder(treenode node)
public static void main(string args)
}
814 二叉樹剪枝
給定二叉樹根結點 root 此外樹的每個結點的值要麼是 0,要麼是 1。返回移除了所有不包含 1 的子樹的原二叉樹。節點 x 的子樹為 x 本身,以及所有 x 的後代。示例1 輸入 1,null,0,0,1 輸出 1,null,0,null,1 解釋 只有紅色節點滿足條件 所有不包含 1 的子樹 右...
LeetCode 二叉樹剪枝 814
給定二叉樹根結點root,此外樹的每個結點的值要麼是 0,要麼是 1。返回移除了所有不包含 1 的子樹的原二叉樹。節點 x 的子樹為 x 本身,以及所有 x 的後代。示例1 輸入 1,null,0,0,1 輸出 1,null,0,null,1 解釋 只有紅色節點滿足條件 所有不包含 1 的子樹 右圖...
Leetcode 814 二叉樹剪枝
time 20190906 type medium 給定二叉樹根結點 root 此外樹的每個結點的值要麼是 0,要麼是 1。返回移除了所有不包含 1 的子樹的原二叉樹。節點 x 的子樹為 x 本身,以及所有 x 的後代。示例1 輸入 1,null,0,0,1 輸出 1,null,0,null,1 解...