[Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
来源:互联网 发布:java calendar. Date 编辑:程序博客网 时间:2024/06/05 20:34
Problem:
I:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7]]II:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3]]Analysis:
The below solutions are recursive methods. The main difference between the below solutions and the typical in-order traseversal is we use an int variable to flag the current level in the process of recursion.
In the second solution, we use the odd-even property of this variable to determine how to add val to the vectors, push_back or insert.
Solutions:
C++:
I:
vector<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > keys; if(root == NULL) return keys; queue<TreeNode*> nodes; vector<int> level; level.push_back(root->val); keys.push_back(level); level.clear(); nodes.push(root); nodes.push(NULL); while(!nodes.empty()) { if(nodes.front() == NULL) { if(!level.empty()) keys.push_back(level); level.clear(); nodes.pop(); continue; } TreeNode* local_root = nodes.front(); nodes.pop(); if(local_root->left) { level.push_back(local_root->left->val); nodes.push(local_root->left); } if(local_root->right) { level.push_back(local_root->right->val); nodes.push(local_root->right); } if(nodes.front() == NULL) nodes.push(NULL); } return keys; }II:
vector<vector<int> > levelOrderBottom(TreeNode *root) { vector<vector<int> > keys; stack<vector<int> > stack_keys; if(root == NULL) return keys; queue<TreeNode*> nodes; vector<int> level; level.push_back(root->val); stack_keys.push(level); level.clear(); nodes.push(root); nodes.push(NULL); while(!nodes.empty()) { if(nodes.front() == NULL) { if(!level.empty()) stack_keys.push(level); level.clear(); nodes.pop(); continue; } TreeNode* local_root = nodes.front(); nodes.pop(); if(local_root->left) { level.push_back(local_root->left->val); nodes.push(local_root->left); } if(local_root->right) { level.push_back(local_root->right->val); nodes.push(local_root->right); } if(nodes.front() == NULL) nodes.push(NULL); } while(!stack_keys.empty()) { keys.push_back(stack_keys.top()); stack_keys.pop(); } return keys; }Java:
Python:
- [Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
- 【Leetcode】Binary Tree Level Order Traversal I/II AND Binary Tree Zigzag Level Order Traversal
- LeetCode题解:Binary Tree Level Order Traversal I and II
- LeetCode Binary Tree Level Order Traversal I and II
- LeetCode 之 Binary Tree Level Order Traversal I and II
- LeetCode.107 Binary Tree Level Order Traversal I&II
- Leetcode 107. Binary Tree Level Order Traversal II (Easy) (cpp)
- LeetCode-Easy刷题(22)Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal I II(102、107)
- LeetCode 102, 103, 107. Binary Tree Level Order Traversal i, ii, Zigzag Level Order
- [LeetCode] Binary tree level order traversal I & II
- 【LeetCode从零单刷】Binary Tree Level Order Traversal I & II
- LeetCode Binary Tree Level Order Traversal I.II
- LeetCode | Binary Tree Level Order Traversal I,II
- 【LeetCode】binary-tree-level-order-traversal i&ii&zigzag
- Binary Tree Level Order Traversal I & II
- Binary Tree Level Order Traversal II I
- Binary Tree Level Order Traversal I & II
- Java—Timer和TimerTask详解(常用API)
- UVA1592 Database
- 如何在 Ubuntu 14.04 中安装 Winusb
- Uva 133 - The Dole Queue
- hdu 1679 The Unique MST 次小生成树 简单题
- [Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
- 翻译 Secrets of the JavaScript Ninja (JavaScript忍者禁术)
- 浏览器标识趣事
- oc中的继承
- Android中图片URI如何转换成对应的存储路径
- 翻译 Secrets of the JavaScript Ninja - 1.进入忍者的世界(1.Enter the ninja)
- 翻译 Secrets of the JavaScript Ninja - 2.测试和debug(2.Testing and debugging)
- MySQL的btree索引和hash索引的区别
- 翻译 Secrets of the JavaScript Ninja - 3.函数是根基(3.Functions are fundamental)