LeetCode - Medium - 814. Binary Tree Pruning

我不是女神ヾ 2022-10-08 05:44 119阅读 0赞

Topic

  • Tree

Description

https://leetcode.com/problems/binary-tree-pruning/

Given the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed.

A subtree of a node node is node plus every node that is a descendant of node.

Example 1:
0a21aaf7128ba5a691f0bc88b6b24c76.png

  1. Input: root = [1,null,0,0,1]
  2. Output: [1,null,0,null,1]
  3. Explanation:
  4. Only the red nodes satisfy the property "every subtree not containing a 1".
  5. The diagram on the right represents the answer.

Example 2:
8918491bf7da9b086b5e5439744dfb36.png

  1. Input: root = [1,0,1,0,0,0,1]
  2. Output: [1,null,1,null,1]

Example 3:

3ecc2b4a71a5cd519a7e668ddd7a7f73.png

  1. Input: root = [1,1,0,1,1,0,1,0]
  2. Output: [1,1,0,1,1,null,1]

Constraints:

  • The number of nodes in the tree is in the range [1, 200].
  • Node.val is either 0 or 1.

Analysis

从树底的叶子节点开始由下往上,把不符合题意得节点剔除,因此用到二叉树的后序遍历模式。

方法一:二叉树的后序遍历模式的递归版。

方法二:二叉树的后序遍历模式的迭代版。

Submission

  1. import java.util.LinkedList;
  2. import com.lun.util.BinaryTree.TreeNode;
  3. public class BinaryTreePruning {
  4. //方法一:用到后序遍历模式的递归版
  5. public TreeNode pruneTree(TreeNode root) {
  6. if(root == null) return null;
  7. root.left = pruneTree(root.left);
  8. root.right = pruneTree(root.right);
  9. if(root.val == 0 && root.left == null && root.right == null)
  10. return null;
  11. return root;
  12. }
  13. //方法二:用到后序遍历模式的迭代版
  14. public TreeNode pruneTree2(TreeNode root) {
  15. if(root == null) return null;
  16. LinkedList<TreeNode[]> stack = new LinkedList<>();//需要将节点以及其父节点存入同一栈帧。
  17. TreeNode fakeRoot = new TreeNode(-1, root, null);//对于根节点没有父节点的情况,可以给它弄个临时父节点。临时父节点的左节点指向根节点
  18. TreeNode p = root, lastNodeVisited = null, parent = fakeRoot;
  19. while(!stack.isEmpty() || p != null) {
  20. if(p != null) {
  21. stack.push(new TreeNode[] { parent, p});
  22. parent = p;
  23. p = p.left;
  24. }else {
  25. TreeNode[] peekOne = stack.peek();
  26. if(peekOne[1].right != null && peekOne[1].right != lastNodeVisited) {
  27. parent = peekOne[1];
  28. p = peekOne[1].right;
  29. }else {
  30. if(peekOne[1].val == 0 && peekOne[1].left == null && peekOne[1].right == null) {
  31. if(peekOne[0].left == peekOne[1]) {
  32. peekOne[0].left = null;
  33. }else {
  34. peekOne[0].right = null;
  35. }
  36. }
  37. lastNodeVisited = stack.pop()[1];
  38. }
  39. }
  40. }
  41. return fakeRoot.left;
  42. }
  43. }

Test

  1. public class BinaryTreePruningTest {
  2. @Test
  3. public void test() {
  4. BinaryTreePruning obj = new BinaryTreePruning();
  5. TreeNode original1 = BinaryTree.integers2BinaryTree(1,null,0,0,1);
  6. TreeNode expected1 = BinaryTree.integers2BinaryTree(1,null,0,null,1);
  7. assertTrue(BinaryTree.equals(obj.pruneTree(original1), expected1));
  8. TreeNode original2 = BinaryTree.integers2BinaryTree(1,0,1,0,0,0,1);
  9. TreeNode expected2 = BinaryTree.integers2BinaryTree(1,null,1,null,1);
  10. assertTrue(BinaryTree.equals(obj.pruneTree(original2), expected2));
  11. TreeNode original3 = BinaryTree.integers2BinaryTree(1,1,0,1,1,0,1,0);
  12. TreeNode expected3 = BinaryTree.integers2BinaryTree(1,1,0,1,1,null,1);
  13. assertTrue(BinaryTree.equals(obj.pruneTree(original3), expected3));
  14. }
  15. @Test
  16. public void test2() {
  17. BinaryTreePruning obj = new BinaryTreePruning();
  18. TreeNode original1 = BinaryTree.integers2BinaryTree(1,null,0,0,1);
  19. TreeNode expected1 = BinaryTree.integers2BinaryTree(1,null,0,null,1);
  20. assertTrue(BinaryTree.equals(obj.pruneTree2(original1), expected1));
  21. TreeNode original2 = BinaryTree.integers2BinaryTree(1,0,1,0,0,0,1);
  22. TreeNode expected2 = BinaryTree.integers2BinaryTree(1,null,1,null,1);
  23. assertTrue(BinaryTree.equals(obj.pruneTree2(original2), expected2));
  24. TreeNode original3 = BinaryTree.integers2BinaryTree(1,1,0,1,1,0,1,0);
  25. TreeNode expected3 = BinaryTree.integers2BinaryTree(1,1,0,1,1,null,1);
  26. assertTrue(BinaryTree.equals(obj.pruneTree2(original3), expected3));
  27. }
  28. }

发表评论

表情:
评论列表 (有 0 条评论,119人围观)

还没有评论,来说两句吧...

相关阅读