[Leetcode 102 and 107, Easy] Binary Tree Level Order Traversal (I and II)
2015-02-23 12:52
543 查看
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
return its level order traversal as:
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
return its bottom-up level order traversal as:
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:
Python:
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 107 Binary Tree Level Order Traversal II(难易度:Easy)
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
- LeetCode题解:Binary Tree Level Order Traversal I and II
- LeetCode107——Binary Tree Level Order Traversal II
- LeetCode 107: Binary Tree Level Order Traversal II
- leetcode || 107、Binary Tree Level Order Traversal II
- leetcode_question_107 Binary Tree Level Order Traversal II
- Leetcode_107_Binary Tree Level Order Traversal II
- [LeetCode 107] Binary Tree Level Order Traversal II
- 【leetcode c++】107 Binary Tree Level Order Traversal II
- LeetCode107 Binary Tree Level Order Traversal II
- LeetCode 107 Binary Tree Level Order Traversal II
- LeetCode: 102_Binary Tree Level Order Traversal | 二叉树自顶向下的层次遍历 | Easy
- [LeetCode]107 Binary Tree Level Order Traversal II
- leetcode_107题——Binary Tree Level Order Traversal II (二叉树,广度优先搜索,队列,栈)
- LeetCode---(107)Binary Tree Level Order Traversal II
- LeetCode(107)Binary Tree Level Order Traversal II
- LeetCode: Binary Tree Level Order Traversal II [107]
- LeetCode 107:Binary Tree Level Order Traversal II
- LeetCode 107 Binary Tree Level Order Traversal II