[leetcode刷题系列]Flatten Binary Tree to Linked List
来源:互联网 发布:福州市网络家长学校 编辑:程序博客网 时间:2024/05/29 17:27
模拟题,不过以前确实没见到过这个类型的题目。 又练习了下指针:)
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { TreeNode * dfs(TreeNode * root){ if(root->left != 0){ TreeNode * last_left = dfs(root->left); if(root->right == 0){ root->right = root->left; root->left = 0; return last_left; }else{ TreeNode * last_right = dfs(root->right); last_left->right = root->right; root->right = root->left; root->left = 0; return last_right; } } if(root->right != 0) return dfs(root->right); return root; }public: void flatten(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == 0) return ; dfs(root); }};
- [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
- [leetcode]Flatten Binary Tree to Linked List
- LeetCode-Flatten Binary Tree to Linked List
- [leetcode] Flatten Binary Tree to Linked List
- 【JavaScript内置函数】[转载]
- lpc1768 IAP疑点全解释
- Android报错 Attempted to access a cursor after it has been closed.的解决方法
- 设计模式
- hdu--1272--并查集(捏个捏个)
- [leetcode刷题系列]Flatten Binary Tree to Linked List
- hdu 1075 What Are You Talking About(HashMap水过,可用字典树)
- hdu--1811--并查集。。。半成品
- =null与is null的区别
- 心情
- vim正则表达式
- 一通前雅虎工程師凌晨三點接到的電話,如何改變了Hadoop的未來?
- hdu--1999--水
- java基础2