1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Solution { public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } private TreeNode leftLast = null; public TreeNode Convert(TreeNode pRootOfTree) { if(pRootOfTree==null){ return null; } if(pRootOfTree.left==null&&pRootOfTree.right==null){ leftLast = pRootOfTree; return pRootOfTree; } TreeNode left = Convert(pRootOfTree.left); if(left!=null){ leftLast.right = pRootOfTree; pRootOfTree.left = leftLast; } leftLast = pRootOfTree.right!=null?pRootOfTree.right:pRootOfTree; TreeNode right = Convert(pRootOfTree.right); if(right!=null){ right.left = pRootOfTree; pRootOfTree.right = right; } return left!=null?left:pRootOfTree; } }
|