常見演算法 - 二叉搜尋樹與雙向連結串列
阿新 • • 發佈:2018-12-20
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { //左側子樹的尾節點 public static TreeNode leftLast; public TreeNode Convert(TreeNode root) { if (root == null){ return null; } if (root.left == null && root.right == null) { leftLast = root; return root; } TreeNode left = Convert(root.left); if(left != null) { leftLast.right = root; root.left = leftLast; } leftLast = root; TreeNode right = Convert(root.right); if(right != null) { root.right = right; right.left = root; } return left != null ? left : root; } }