Leetcode:637. 二叉树的层平均值】的更多相关文章

Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };…
637. 二叉树的层平均值 637. Average of Levels in Binary Tree LeetCode637. Average of Levels in Binary Tree 题目描述 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第 0 层的平均值是 3,第 1 层是 14.5,第 2 层是 11.因此返回 [3, 14.5, 11]. 注意: 节点值的范围…
637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第0层的平均值是 3, 第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11]. 注意: 节点值的范围在32位有符号整数范围内. /** * Definition for a binary tree node. * public class TreeNode { * int val;…
题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, a…
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and o…
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and o…
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and o…
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode* root) { vector<double> res; if(root == NULL) return res; queue<TreeNode*> q; q.push(root); while(!q.empty()) { int len = q.size(); double sum…
题目 1 class Solution { 2 public: 3 vector<double>ans; 4 vector<double> averageOfLevels(TreeNode* root) { 5 if(!root) return ans; 6 queue<TreeNode*>q; 7 q.push(root); 8 while(!q.empty()){ 9 int num = q.size();double sum = 0; 10 for(int i =…
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每日一算法2019/5/11Day 8LeetCode102. Binary Tree Level Order Traversal 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层次遍历结果: [ [3], [9,20], [15…
1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level_data 和一个存放每一层节点的队列 node_queue. 如果根节点非空,根节点进队,然后循环以下过程直至队列为空: 得到队列的大小,即为树中当前层的节点个数.队列元素循环出队,并将节点的值加入 level_data,如果节点有左右子节点,左右子节点入队 将 level_data 插入到 da…
LeetCode:二叉树的层次遍历||[107] 题目描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其自底向上的层次遍历为: [ [15,7], [9,20], [3] ] 题目分析 依然是二叉树的层次遍历,采取BFS算法,最后的逆序只是一个小插曲而已. Java题解 /** * Definition for a b…
LeetCode:二叉树的锯齿形层次遍历[103] 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回锯齿形层次遍历如下: [ [3], [20,9], [15,7] ] 题目分析 层次遍历,应该很容易想到BFS(宽度优先搜索算法),此处是锯齿形,即一层是从左往右,下一层就是从右往左. 解决办法是每一层都…
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and o…
LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 题目分析 这道题难度和N叉树的后序遍历是等同的,但是标注为困难. 首先我们都知道,栈顶元素一般都是根元素,弹出根元素,加入根元素的左节点.右节点后,位于栈顶的是根元素的最右子节点,栈低的是根元素的最左子节点. 如果我们按照这个顺序打印的话,输出的是[1,2,3].如…
LeetCode:二叉树的前序遍历[144] 题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 题目分析 如果用递归代码的话,很简单,先序遍历,就是先遍历当前节点,接着是左孩子然后是右孩子,到每个孩子都是这样的处理过程. public void preorder(TreeNode root,List<Integer> res) { if(root==null) return; res.add(root.val…
LeetCode:二叉树相关应用 基础知识 617.归并两个二叉树 题目 Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge ru…
题目 二叉树按层打印和ZigZag打印 java代码 package com.lizhouwei.chapter3; import java.util.LinkedList; import java.util.Queue; /** * @Description:二叉树按层打印和ZigZag打印 * @Author: lizhouwei * @CreateDate: 2018/4/14 21:47 * @Modify by: * @ModifyDate: */ public class Chapt…
LeetCode:二叉树剪枝[814] 题目描述 给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1. 返回移除了所有不包含 1 的子树的原二叉树. ( 节点 X 的子树为 X 本身,以及所有 X 的后代.) 示例1: 输入: [1,null,0,0,1] 输出: [1,null,0,null,1] 解释: 只有红色节点满足条件“所有不包含 1 的子树”. 右图为返回的答案. 示例2: 输入: [1,0,1,0,0,0,1] 输出: [1,null,1,null,1] 示例…
二叉树地序列化和反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实现二叉树的序列化与反序列化.这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构. 示例:  你可以将以下二叉树: 序列化为 "[1,2,3,null,null,4,5]" 提示: 这与 Le…
297. 二叉树的序列化与反序列化 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实现二叉树的序列化与反序列化.这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构. 示例: 你可以将以下二叉树: 1 / \ 2 3 / \ 4 5 序列化为 "[1,2,3,null,nul…
1.题目描述 Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. 计算二叉树每一层的节点的数据域的平均值. 2.题目分析 使用广度优先遍历方法,逐层对二叉树进行访问,求均值.使用一个队列数据结构完成对二叉树的访问,队列(queue)的特点是FIFO,即先进先出,queue的几种常用的方法是: queue::front()  :访问队首…
需求: 以层遍历一棵二叉树,二叉树的结点结构如下 struct tree_node{ struct tree_node *lc; struct tree_node *rc; int data; }; 例如: //   1 //   / \ // 2   3 //     / //    4 //     \ //      5 层遍历后输出1,2,3,#,#,4,#,#,5,#代表该结点为空. 要求:空间复杂度为O(n),时间复杂度为O(n) 思路: 使用一个大小为n的数组,以逐层结点方式记录该…
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). LeetCode103. Binary Tree Zigzag Level Order Traversal中等 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回锯齿形层次遍历如…
给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K . 返回到目标结点 target 距离为 K 的所有结点的值的列表. 答案可以以任何顺序返回. 示例 1: 输入:root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2 输出:[7,4,1] 解释: 所求结点为与目标结点(值为 5)距离为 2 的结点, 值分别为 7,4,以及 1 注意,输入的 "root" 和 "target&qu…
二叉树,结构很简单,只是比单链表复杂了那么一丢丢而已.我们先来看看它们结点上的差异: /* 单链表的结构 */ struct SingleList{ int element; struct SingleList *next; }; /* 二叉树的结构 */ struct BinaryTree{ int element; struct BinaryTree *left; struct BinaryTree *right; }; 根据以上两个结构,我们不难发现,单链表的结点只有一个指向下一结点的指针…
1.二进制定义 typedef struct BTreeNodeElement_t_ { void *data; } BTreeNodeElement_t; typedef struct BTreeNode_t_ { BTreeNodeElement_t *m_pElemt; struct BTreeNode_t_ *m_pLeft; struct BTreeNode_t_ *m_pRight; } BTreeNode_t; 2.求二叉树第K层的节点数 (1)递归方式: 给定根节点pRoot:…
二叉树天然的递归特性,使得我们可以使用递归算法对二叉树进行遍历和重建.之前已经写过LeetCode二叉树的前序.中序.后序遍历(递归实现),那么本文将进行二叉树的重建,经过对比,会发现二者有着许多相似之处. 准备工作 二叉树节点定义: //Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }…
题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. 题目分析及思路 给定一棵非空二叉树,要求以列表的形式返回每一层结点值的平均值.可以使用队列保存结点,进行层次遍历,要特别注意空结点的判断. python代码 # Definition for a binary tree node. # class TreeNode: # …
958. 二叉树的完全性检验  显示英文描述 我的提交返回竞赛   用户通过次数119 用户尝试次数157 通过次数123 提交次数378 题目难度Medium 给定一个二叉树,确定它是否是一个完全二叉树. 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树.(注:第 h 层可能包含 1~ 2h 个节点.) 示例 1: 输入:[1,2,3,4,5,6] 输出:tru…