[关闭]
@xmruibi 2015-04-04T00:12:16.000000Z 字数 3027 阅读 808

Tree Hard Problem

Algorithm


1. Path Sum Maximum

For each node like following, there should be four ways existing for max path:
此处输入图片的描述

Idea:

  1. class solution{
  2. int maxVal;
  3. public int maxPathSum(TreeNode root) {
  4. maxVal = Integer.MIN_VALUE;
  5. helper(root, maxVal);
  6. return maxVal;
  7. }
  8. private int helper(TreeNode node , int maxVal) {
  9. if(node==null)
  10. return 0;
  11. int left = helper(node.left, maxVal);
  12. int right = helper(node.right, maxVal);
  13. int passArch = left + right + node.val;
  14. int nopass = Math.max(node.val, Math.max(right, left)+node.val);
  15. maxVal =Math.max(maxVal, Math.max(passArch, nopass));
  16. return nopass;
  17. }
  18. }

2. Recover Tree

Key Point:

If we change the value 4 and 8: 1 3 8 6 7 4 10 13 14, when it goes to the node 6, now the pre->val = 8, check if pre6). So we store the first pointer pointing to the node 6 and second pointer pointing to the node 8. To do so, we have stored the wrong nodes, if every other node keep the correct order, then swapping these nodes is enough for the problem. In other words, after the whole traversal, what we need to do is just changing the values of the first and second. Continue our example here, when the traversal goes to the node 4, now the node 7 is its pre, which is also wrong, so the second wrong node is found, and we change the second pointer pointing to node 4.

  1. class RecoverTree {
  2. TreeNode node1 = null, node2 = null, prePtr = null;
  3. // prePtr is the current pre-node on preorder
  4. // node1, node2 are the two nodes need to be swap
  5. public void recoverTree(TreeNode root) {
  6. if (root == null)
  7. return;
  8. dfsFinder(root);
  9. swap(node1, node2);
  10. }
  11. // preorder traversal
  12. private void dfsFinder(TreeNode node) {
  13. if (node == null)
  14. return;
  15. if (node.left != null)
  16. dfsFinder(node.left);
  17. if (prePtr != null && prePtr.val > node.val) {
  18. if (node1 == null)
  19. node1 = prePtr; // first node in first time reverse order
  20. // detected
  21. node2 = node; // second node in reverse order for second time
  22. // detected
  23. }
  24. prePtr = node; // update all the time for prePointer
  25. dfsFinder(node.right);
  26. }
  27. private void swap(TreeNode node1, TreeNode node2) {
  28. int tmp = node1.val;
  29. node1.val = node2.val;
  30. node2.val = tmp;
  31. }
  32. }

3. Populating Next Right Pointers in Each Node II

  1. class PopulatingNextRightPointersII {
  2. public void connect(TreeLinkNode root) {
  3. if (root == null)
  4. return;
  5. TreeLinkNode lastHead = root; // last level head node(the parent of
  6. // current node)
  7. TreeLinkNode pre = null; // current previous pointer
  8. TreeLinkNode curHead = null; // current level head node
  9. while (lastHead != null) {
  10. TreeLinkNode lastCur = lastHead;
  11. while (lastCur != null) {
  12. // in this while loop, nodes are made up in the same line
  13. // each time to make sure the curhead is null or not null
  14. // assign curHead once current node has child
  15. if (lastCur.left != null) {
  16. if (curHead == null) {
  17. curHead = lastCur.left;
  18. pre = curHead;
  19. } else {
  20. pre.next = lastCur.left;
  21. pre = pre.next;
  22. }
  23. }
  24. if (lastCur.right != null) {
  25. if (curHead == null) {
  26. /**
  27. * for situation:
  28. * O (lastCur) ---> O
  29. * / \ / \
  30. * null O(nextHead) O O
  31. */
  32. curHead = lastCur.right;
  33. pre = curHead;
  34. } else {
  35. // this is normal case;
  36. pre.next = lastCur.right;
  37. pre = pre.next;
  38. }
  39. }
  40. lastCur = lastCur.next; // change to next parent
  41. }
  42. // do next line
  43. lastHead = curHead;
  44. curHead=null;
  45. }
  46. }
  47. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注