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

LeetCode -- Flatten 二叉树

时间:2015-08-11 16:16:09      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:

这个题目主要考察二叉树的先序遍历。


1. 先序遍历
2. 节点用队列存储
3. 遍历队列,建立链表


实现:



public class Solution {
public void Flatten(TreeNode root) 
 {
     if(root == null)
	 {
		return;
	 }
	 Travel(root);
	 
	 root = _nodes[0];
	 root.left = null;
	 root.right = null;
	 for(var i = 1;i < _nodes.Count; i++){
			var l = _nodes[i];
			l.left = null;
			l.right = null;
			root.right = l;
			root = root.right;
	 }
 }
	
	private List<TreeNode> _nodes = new List<TreeNode>();
	private void Travel(TreeNode root){
		_nodes.Add(root);
		if(root.left != null){
			Travel(root.left);
		}
		if(root.right != null){
			Travel(root.right);
		}
	}
    
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode -- Flatten 二叉树

标签:

原文地址:http://blog.csdn.net/lan_liang/article/details/47420913

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