leetcode Flatten Binary Tree to Linked List

来源:互联网 发布:工业设计必用的软件 编辑:程序博客网 时间:2024/05/22 12:53

题目链接

思路:
用一个递归思路,很简单

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public void flatten(TreeNode root) {      if(root==null)        {            return;        }        flatten(root.left);        TreeNode right=root.right;        if(root.left!=null)        {            TreeNode temp=root.left;            while(temp.right!=null)            {                temp=temp.right;            }            root.right=root.left;            temp.right=right;        }        root.left=null;        flatten(right);    }}
0 0
原创粉丝点击