[LeetCode] Binary Tree Level Order Traversal

A classic tree traversal problem. I share my two solutions here: BFS and DFS.

BFS:

 1     vector<vector<int>> levelOrder(TreeNode *root) {
 2         vector<vector<int>> levels;
 3         if(!root) return levels;
 4         queue<TreeNode*> toVisit;
 5         toVisit.push(root);
 6         int numLevelNodes = 1;
 7         while(!toVisit.empty()) {
 8             vector<int> level;
 9             for (int i = 0; i < numLevelNodes; i++) {
10                 TreeNode* node = toVisit.front();
11                 toVisit.pop();
12                 level.push_back(node -> val);
13                 if(node -> left) toVisit.push(node -> left);
14                 if(node -> right) toVisit.push(node -> right);
15             }
16             levels.push_back(level);
17             numLevelNodes = toVisit.size();
18         }
19         return levels;
20     }

DFS:

 1 vector<vector<int>> levelOrder(TreeNode *root) {
 2     vector<vector<int>> levels;
 3     if(!root) return levels;
 4     int curLevel = 1;
 5     bool nextLevel = true;
 6     while(nextLevel) {
 7         nextLevel = false;
 8         vector<int> level;
 9         levelTraverse(root, curLevel++, nextLevel, level);
10         levels.push_back(level);
11     }
12     return levels;
13 }
14 void levelTraverse(TreeNode* node, int curLevel, bool& nextLevel, vector<int>& level) {
15     if(!node) return;
16     if(curLevel == 1) {
17         level.push_back(node -> val);
18         if(node -> left || node -> right) nextLevel = true;
19     }
20     else {
21         levelTraverse(node -> left, curLevel - 1, nextLevel, level);
22         levelTraverse(node -> right, curLevel - 1, nextLevel, level);
23     }
24 }
时间: 2024-10-16 12:26:59

[LeetCode] Binary Tree Level Order Traversal的相关文章

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-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

LeetCode——Binary Tree Level Order Traversal

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] ] 原题链接:

LeetCode——Binary Tree Level Order Traversal 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 trave

LeetCode: Binary Tree Level Order Traversal II [107]

[题目] 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

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal 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 re

LeetCode: Binary Tree Level Order Traversal 解题报告

Binary Tree Level Order Traversal 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   7return its leve

leetcode Binary Tree Level Order Traversal I II

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] ] /** *

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树

Binary Tree Level Order Traversal 题目描述: 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 trav

LeetCode: Binary Tree Level Order Traversal &amp;&amp; Binary Tree Zigzag Level Order Traversal

Title: 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] ]