码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode#114Flatten Binary Tree to Linked List

时间:2015-05-22 01:57:56      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:example   linked   public   二叉树   should   

Flatten Binary Tree to Linked List

 Total Accepted: 46999 Total Submissions: 163018My Submissions

Question Solution 


Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        /        2   5
      / \        3   4   6


The flattened tree should look like:

   1
         2
             3
                 4
                     5
                         6

click to show hints.


分析:先序遍历,将二叉树转变为左支树


public class Solution {

    TreeNode H;

    void TravelTree(TreeNode x){

        H.right=x;

        H.left=null;

        H=H.right;

        

        TreeNode l=x.left;

        TreeNode r=x.right;

        x.left=null;

        x.right=null;

        if(l!=null)

            TravelTree(l);

        if(r!=null)

            TravelTree(r);

    }

    

    public void flatten(TreeNode root) {

        if(root!=null)

        {

            H=root;

            TreeNode l=root.left;

            TreeNode r=root.right;

            root.left=null;

            root.right=null;

            if(l!=null)

                TravelTree(l);

            if(r!=null)

                TravelTree(r);

        }

        /*

        Stack<TreeNode> s1=new Stack<TreeNode>();

        Stack<TreeNode> s2=new Stack<TreeNode>();

        

        if(root!=null)

        {

            //TreeNode head=new TreeNode(0);

            TreeNode h=root;

            if(root.left!=null)

                s1.push(root.left);

            if(root.right!=null)

                s1.push(root.right);

            while(!s1.isEmpty())

            {

                TreeNode p=s1.peek();

                s2.push(p);

                s1.pop();

                int mini=p.val;

                TreeNode minitn=p;

                while(!s1.isEmpty())

                {

                    p=s1.peek();

                    s2.push(p);

                    s1.pop();

                    if(p.val<mini)

                    {

                        mini=p.val;

                        minitn=p;

                    }

                }

                

                while(!s2.isEmpty())

                {

                    p=s2.peek();

                    if(p==minitn)

                    {

                        h.right=p;

                        h.left=null;

                        h=h.right;

                        s2.pop();

                        if(p.left!=null)

                            s1.push(p.left);

                        if(p.right!=null)

                            s1.push(p.right);

                    }

                    else

                    {

                        s2.pop();

                        s1.push(p);

                    }

                }

                

            }

        }

        */

    }

}


Leetcode#114Flatten Binary Tree to Linked List

标签:example   linked   public   二叉树   should   

原文地址:http://7061299.blog.51cto.com/7051299/1653678

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!