LeetCode:M-114. Flatten Binary Tree to Linked List
来源:互联网 发布:装修公司源码 编辑:程序博客网 时间:2024/06/05 16:10
LeetCode链接
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
1、对于当前访问的node,假设左右两子树均为符合要求的展开链表
2、函数helper返回值为当前树展开后的尾部node
3、根据左右子树返回的链表尾部node,将当前父节点与左右子树串联成新的链表
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public void flatten(TreeNode root) { helper(root); } public TreeNode helper(TreeNode node){ if(node==null) return null; if(node.left==null && node.right==null) return node; TreeNode leftTail=null; if(node.left!=null){ leftTail=helper(node.left); } TreeNode rightTail=null; if(node.right!=null){ rightTail=helper(node.right); } if(node.left!=null){ leftTail.right = node.right; node.right = node.left; node.left = null; } if(rightTail!=null){ return rightTail; }else{ return leftTail; } }}
阅读全文
0 0
- LeetCode:M-114. Flatten Binary Tree to Linked List
- 【M】【83】【leetcode题解】Flatten Binary Tree to Linked List
- (M)DFS:114. 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
- [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
- 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
- [leetcode]Flatten Binary Tree to Linked List
- LeetCode-Flatten Binary Tree to Linked List
- [leetcode] Flatten Binary Tree to Linked List
- linux下修改tomcat默认访问主页
- 20170925_死锁的产生和消除
- 无法解析的外部符号问题小结
- angularjs 获取 ResponseEntity<String>
- Effective Java(数组和泛型的实现方式、用无限制的通配符提高API的灵活性)
- LeetCode:M-114. Flatten Binary Tree to Linked List
- WPF Binding值转换器ValueConverter使用简介(一)
- jitter in fMRI design
- C#学习笔记13-抽象类和接口
- MATLAB中cell数组的全面介绍
- U盘安装镜像WIN10 64 专业版 BMR转GPT 的问题
- Maven的安装
- Google支付服务端client_id和client_secret及refresh_token参数申请
- Hadoop完全分布式搭建过程