二叉树 层次遍历 (queue)遍历的变式题(leetcode)
2016-10-04 12:35
447 查看
利用队列结构和二叉树,变换求解
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
与 116 题一致
ac代码如下
https://leetcode.com/problems/binary-tree-right-side-view/
题目描述
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
You should return [1, 3, 4].
ac代码如下
116. Populating Next Right Pointers in Each Node
题目地址https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
117. Populating Next Right Pointers in Each Node II
题目地址https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
与 116 题一致
ac代码如下
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} * }; */ class Solution { public: void connect(TreeLinkNode *root) { if (root == NULL) return; queue<TreeLinkNode*> que; queue<int> queC; que.push(root); queC.push(1); while (!que.empty()) { TreeLinkNode* top = que.front(); que.pop(); int ceng = queC.front(); queC.pop(); if (que.empty()) { top->next = NULL; } else if (ceng == queC.front()) { top->next = que.front(); } else{ top->next = NULL; } if (top->left != NULL) { que.push(top->left); queC.push(ceng + 1); } if (top->right != NULL) { que.push(top->right); queC.push(ceng + 1); } }// end while } };
199. Binary Tree Right Side View
题目地址https://leetcode.com/problems/binary-tree-right-side-view/
题目描述
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return [1, 3, 4].
ac代码如下
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int Height(TreeNode* root){ if (root == NULL) return 0; else if (root->left == NULL && root->right == NULL) return 1; else{ return 1 + max(Height(root->left), Height(root->right)); } } vector<int> rightSideView(TreeNode* root) { vector<int> ans; if (root == NULL) return ans; int h = Height(root); queue<TreeNode*> que; queue<int> queCeng; que.push(root); queCeng.push(1); int last = 0; while (!que.empty()) { TreeNode* top = que.front(); que.pop(); int ceng = queCeng.front(); queCeng.pop(); if (ceng != last) { ans.push_back(top->val); if (ceng == h) break; last = ceng; } if (top->right != NULL) { que.push(top->right); queCeng.push(ceng + 1); } if (top->left != NULL) { que.push(top->left); queCeng.push(ceng + 1); } } return ans; } };
相关文章推荐
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
- Leetcode#107. Binary Tree Level Order Traversal II (queue二叉树层次遍历)
- LeetCode - 107. 二叉树的层次遍历 II
- LeetCode基础--二叉树--层次遍历
- LeetCode: 103_Binary Tree Zigzag Level Order Traversal | 二叉树Zigzag层次遍历 | Medium
- [leetcode-二叉树层次遍历并统计每层节点数]--107. Binary Tree Level Order Traversal II
- 【LeetCode】BinaryTreeLevelOrderTraversal_ii 二叉树的层次遍历 II
- 二叉树层次遍历:Queue+每一层一个NULL标记
- leetcode-二叉树的层次遍历(Java)
- LeetCode103 BinaryTreeZigzagLevelOrderTraversal(二叉树Z形层次遍历) Java题解
- LeetCode 102 Binary Tree Level Order Traversal(二叉树的层次遍历)
- 103.leetcode Binary Tree Zigzag Level Order Traversal(medium)[二叉树层次遍历 栈]
- [LeetCode]二叉树层次遍历讲解与实例
- LeetCode(Binary Tree Level Order Traversal, 2,Zigzag)二叉树的层次遍历
- leetcode 二叉树层次遍历输出Binary Tree Level Order Traversal
- 199.leetcode Binary Tree Right Side View(medium)[层次遍历二叉树 队列]
- Leetcode 107. Binary Tree Level Order Traversal II 二叉树层次遍历2 解题报告
- Leetcode 107 Binary Tree Level Order Traversal II 二叉树层次遍历与倒转
- 【LeetCode】Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历 - Medium(LinkedIn)