Flatten Binary Tree to Linked List
来源:互联网 发布:mac修改淘宝登录密码 编辑:程序博客网 时间:2024/06/01 09:46
Flatten a binary tree to a fake "linked list" in pre-order traversal.
Here we use the right pointer in TreeNode as the nextpointer in ListNode.
该题目的重点是使用递归的方式寻找左右子树的最后一点
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /* * @param root: a TreeNode, the root of the binary tree * @return: */ public void flatten(TreeNode root) { // write your code here if (root == null) { return; } util(root); } private TreeNode util(TreeNode root) { if (root == null) { return null; } TreeNode leftLast = util(root.left); TreeNode rightLast = util(root.right); if (leftLast != null) { leftLast.left = null; leftLast.right = root.right; root.right = root.left; root.left = null; } if (rightLast != null) { return rightLast; } if (leftLast != null) { return leftLast; } return root; }}
阅读全文
0 0
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- ReentrantLock源码分析
- 文章标题
- IO流学习记录
- 动态规划 计算二项式系数
- RCNN算法的tensorflow实现
- Flatten Binary Tree to Linked List
- 关于resource加载配置文件问题
- poj 1716 Integer Intervals
- Windows和Mac OS下清除登录共享网络、共享文件的密码
- 点滴记录:大量数据时,系统性能的优化
- win32汇编环境搭建以及第一个程序
- java8系列之重新认识hasMap
- poj 1465 求一个数的最小倍数满足一个条件
- 背包算法