/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { TreeNode last = null; public void flatten(TreeNode root) { if(root == null){ return ; } //递归过程 flatten(root.left); flatten(root.right); //处理过程 //将root的右子树放到root左子树的最右子树上,然后将root的左子树连到root.right //同时将左子树置空,然后将指针指向root.right,向下继续进行 if(root.left != null){ TreeNode pre = root.left; while(pre.right != null){ pre = pre.right; } pre.right = root.right; root.right = root.left; root.left = null; } root = root.right; } }
114
猜你喜欢
转载自www.cnblogs.com/zhaijiayu/p/11595094.html
今日推荐
周排行