【leetcode】114.(Medium) Flatten Binary Tree to Linked List
解題思路:
和morris遍歷差不多
提交程式碼:
class Solution {
public void flatten(TreeNode root) {
TreeNode p1=root,p2;
while(p1!=null) {
if(p1.left!=null) {
p2=p1.left;
while(p2.right!=null&&p2.right!=p1)
p2=p2.right;
if(p2.right==null) {
p2. right=p1.right;
p1.right=p1.left;
p1.left=null;
}else p2.right=null;
p1=p1.right;
}else {
p1=p1.right;
}
}
}
}
執行結果:
相關推薦
【leetcode】114.(Medium) Flatten Binary Tree to Linked List
解題思路: 和morris遍歷差不多 提交程式碼: class Solution { public void flatten(TreeNode root) { TreeNode p1=root,p2; while(p1!=null) {
【leetcode】94.(Medium)Binary Tree Inorder Traversal
解題思路: morris遍歷 提交程式碼:回溯 class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ans=n
【leetcode】103.(Medium)Binary Tree Zigzag Level Order Traversal
解題思路: 維護兩個棧,輪番記錄每一層的結點 時間複雜度:O(n) 空間複雜度:O(n) 提交程式碼: class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNod
【leetcode】102.(Medium)Binary Tree Level Order Traversal
解題思路: 用兩個佇列輪番記錄每一層的結點 時間複雜度:O(n) 空間複雜度:O(n) 提交程式碼: class Solution { public List<List<Integer>> levelOrder(TreeNode root)
【LeetCode-面試演算法經典-Java實現】【114-Flatten Binary Tree to Linked List(二叉樹轉單鏈表)】
原題 Given a binary tree, flatten it to a linked list in-place. For example, Given
114. Flatten Binary Tree to Linked List【Medium】【將給定的二叉樹轉化為“只有右孩子節點”的鏈表(樹)】
bmi 轉化 water public fin linked in-place ould baidu Given a binary tree, flatten it to a linked list in-place. For example, given the
【LeetCode】114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4
【leetcode】Flatten Binary Tree to Linked List
模式 分析 事情 oot left stat log 這樣的 要求 分析: 問題是將給定的二叉樹變換成令一種形式,這樣的類型的問題。其模式是,將左子樹變換成某種形式
LeetCode 114. 二叉樹展開為鏈表(Flatten Binary Tree to Linked List)
tree binary ont 基本思想 oot public ike 返回 上一個 題目描述 給定一個二叉樹,原地將它展開為鏈表。 例如,給定二叉樹 1 / 2 5 / \ 3 4 6 將其展開為: 1 2 3
【LeetCode】68. Flatten Binary Tree to Linked List
題目描述(Medium) Given a binary tree, flatten it to a linked list in-place. 題目連結 Example 1: For example, given the following tree:
【leetcode】145.(Hard)Binary Tree Postorder Traversal
解題思路: morris後序遍歷 這篇部落格的程式碼挺好的: https://blog.csdn.net/zuochao_2013/article/details/78538729 時間複雜度:O(n) morris演算法會遍歷每個節點的左子樹的右邊界兩次,所有的右邊界加起來是所有的結點
【leetcode】124.(Hard)Binary Tree Maximum Path Sum
解題思路: 回溯 對於樹的一個結點來說,[左子樹值]、[右子樹值]、[左子樹+右子樹+當前結點值],這三種組合中,必然有一個最大值,將這個最大值儲存在maxValue[0]中,實時更新即可。 函式maxValue返回的是如果一定要包含當前結點值時的最大數值。 提交程式碼: cl
JavaScript刷LeetCode -- 114. Flatten Binary Tree to Linked List [Medium]
一、題目 Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \
LeetCode:114. Flatten Binary Tree to Linked List(固定二叉樹為連結串列)
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The f
【leetcode】129.(Medium)Sum Root to Leaf Numbers
解題思路: 思路一: 使用DFS,然後用一個list(記為curPath)來記錄當前路徑的所有數值,當訪問到根節點時將curPath中的數值轉換為一個整型數值,計入到結果(res)中。 提交程式碼: class Solution { public int sum
LeetCode 114. Flatten Binary Tree to Linked List
left end anything 實現 fin 後序遍歷 sel [] you 原題 Given a binary tree, flatten it to a linked list in-place. For example,Given 1
[LeetCode] 114. Flatten Binary Tree to Linked List Java
ini like lin ont == lis example 遞歸 font 題目: Given a binary tree, flatten it to a linked list in-place. For example,Given 1
leetcode--114. Flatten Binary Tree to Linked List
turn spa preorder 先序 reorder 復雜 treenode 時間 lin 1、問題描述 Given a binary tree, flatten it to a linked list in-place. For example,Given
LeetCode 114 Flatten Binary Tree to Linked List
assign discuss may pla bee accepted 寫在前面 lis 數據 寫在前面 斷斷續續刷20多道LeetCode上的題了,之所以從這道題尅是記錄,是終於自己慢慢有思路了,不再參考一些discuss中的思路了,還有,終於一次通過編譯,L
[leetcode]114. Flatten Binary Tree to Linked List將二叉樹展成一個連結串列
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened