@xmruibi
2015-04-04T00:12:16.000000Z
字数 3027
阅读 808
Algorithm
For each node like following, there should be four ways existing for max path:

class solution{int maxVal;public int maxPathSum(TreeNode root) {maxVal = Integer.MIN_VALUE;helper(root, maxVal);return maxVal;}private int helper(TreeNode node , int maxVal) {if(node==null)return 0;int left = helper(node.left, maxVal);int right = helper(node.right, maxVal);int passArch = left + right + node.val;int nopass = Math.max(node.val, Math.max(right, left)+node.val);maxVal =Math.max(maxVal, Math.max(passArch, nopass));return nopass;}}
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.
class RecoverTree {TreeNode node1 = null, node2 = null, prePtr = null;// prePtr is the current pre-node on preorder// node1, node2 are the two nodes need to be swappublic void recoverTree(TreeNode root) {if (root == null)return;dfsFinder(root);swap(node1, node2);}// preorder traversalprivate void dfsFinder(TreeNode node) {if (node == null)return;if (node.left != null)dfsFinder(node.left);if (prePtr != null && prePtr.val > node.val) {if (node1 == null)node1 = prePtr; // first node in first time reverse order// detectednode2 = node; // second node in reverse order for second time// detected}prePtr = node; // update all the time for prePointerdfsFinder(node.right);}private void swap(TreeNode node1, TreeNode node2) {int tmp = node1.val;node1.val = node2.val;node2.val = tmp;}}
class PopulatingNextRightPointersII {public void connect(TreeLinkNode root) {if (root == null)return;TreeLinkNode lastHead = root; // last level head node(the parent of// current node)TreeLinkNode pre = null; // current previous pointerTreeLinkNode curHead = null; // current level head nodewhile (lastHead != null) {TreeLinkNode lastCur = lastHead;while (lastCur != null) {// in this while loop, nodes are made up in the same line// each time to make sure the curhead is null or not null// assign curHead once current node has childif (lastCur.left != null) {if (curHead == null) {curHead = lastCur.left;pre = curHead;} else {pre.next = lastCur.left;pre = pre.next;}}if (lastCur.right != null) {if (curHead == null) {/*** for situation:* O (lastCur) ---> O* / \ / \* null O(nextHead) O O*/curHead = lastCur.right;pre = curHead;} else {// this is normal case;pre.next = lastCur.right;pre = pre.next;}}lastCur = lastCur.next; // change to next parent}// do next linelastHead = curHead;curHead=null;}}}