标签:
这个题目主要考察二叉树的先序遍历。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); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/47420913