国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Leetcode: Binary Tree Right Side View

2019-11-14 23:43:16
字體:
來源:轉載
供稿:網友
Leetcode: 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].

這道題就是BT的Level Order Traversal,每次要換一層的時候,記錄當前節點

 1 /** 2  * Definition for binary tree 3  * public class TreeNode { 4  *     int val; 5  *     TreeNode left; 6  *     TreeNode right; 7  *     TreeNode(int x) { val = x; } 8  * } 9  */10 public class Solution {11     public List<Integer> rightSideView(TreeNode root) {12         ArrayList<Integer> res = new ArrayList<Integer>();13         if (root == null) return res;14         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();15         queue.offer(root);16         int PNum = 1;17         int CNum = 0;18         while (!queue.isEmpty()) {19             TreeNode cur = queue.poll();20             PNum--;21             if (cur.left != null) {22                 queue.offer(cur.left);23                 CNum++;24             }25             if (cur.right != null) {26                 queue.offer(cur.right);27                 CNum++;28             }29             if (PNum == 0) {30                 res.add(cur.val);31                 PNum = CNum;32                 CNum = 0;33             }34         }35         return res;36     }37 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪洞县| 云龙县| 保山市| 古浪县| 新野县| 洮南市| 固镇县| 潞西市| 黄山市| 博罗县| 呼和浩特市| 永兴县| 祁东县| 饶平县| 绥宁县| 卓资县| 凌海市| 乐亭县| 黔西县| 嵊州市| 微博| 潮安县| 敦煌市| 彭泽县| 沾化县| 厦门市| 黔江区| 三台县| 德阳市| 白水县| 昭通市| 泾川县| 丹巴县| 安远县| 阿合奇县| 苏尼特右旗| 枣强县| 西峡县| 新邵县| 桑植县| 南丰县|