自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(110)
  • 收藏
  • 关注

原创 Sum of Left Leaves

Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24

2016-10-14 14:23:12 406

原创 快速排序实现

/************************************************************************* > File Name: qucksort.cpp > Author: chsun > Mail: > Created Time: 2016年08月20日 星期六 10时13分13秒 ****************************

2016-08-20 15:41:21 290

原创 求1+2+3+...+n

题目描述求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。方法一:基于公式n(n+1)/2来计算最后的结果class Solution {public: int Sum_Solution(int n) { /** 方法一

2016-08-19 16:30:16 330

原创 linux下动态链接库和静态链接库的使用和区别

字面意思上来看,一看是在程序运行的时候动态链接。而另一个是在编译的时候静态链接。考虑下面的3个文件,hello.c,hello.h,main.chello.h#ifndef _HELLO_H#define _HELLO_Hvoid hello();#endifhello.cpp#includeusing namespace std;void hello(){ c

2016-07-21 15:45:59 675

原创 关于c++对象模型中指出的c++新手容易出现的误解之编译器不同处理

先说书上提到的关于c++新手常见的误解:1.任何class如果没有定义default constructor的话,就会被合成一个来2.编译器合成出来的default constructor会显示设定class中每一个data member的默认值在前面Lippman大神已经指出,编译器合成默认构造函数只有如下四种情况(1)含有对象成员,对象成员有默认构造函数(2)基类有默认构造

2016-07-18 16:29:16 316

原创 构造函数语义学

c++在一个类没有申明构造函数的时候会为其申明一个默认的构造函数,但是这个构造函数是无用的,并不会初始化这个类中的内置类型的成员。参考一下代码://默认构造函数测试#includeusing namespace std;class Foo{public: int val; Foo *ptr;};int main(){ Foo foo; if

2016-07-18 11:21:08 775

原创 ubuntu14.04 内核版本4.2下解决vmware安装Virtual ethernet [Failed]的情况记录

出现的情况是这样的,安装完vmware以后,启动的时候提示虚拟网络模块fail,如下所示:在你点击安装以后会出现安装失败的情况首先,明确下自己的系统内核版本,如果是3.7X,可以参考如下步(1)创建一个临时文件,/tmp目录下,命名filter.c.diff(2)复制一下内容在filter.c.diff中205a206> #if LINUX_VERSION_CODE

2016-07-08 11:07:54 2613

原创 条款08 别让异常逃离析构函数

问题:因为析构函数通常干的事情都是清理释放对象持有的资源。如果在析构的过程中发生了异常就可能会导致资源泄露,导致程序出现不明确的行为解决方式:(1)在析构函数中加上异常捕获的代码,析构函数中可能出现异常的部分。当出现异常的时候,使用abort来强迫终止程序,这样可以阻止异常从析构函数中传递出去,如果传递出去可能会导致不明确的行为。也就是说使用abort可以抢先不明确的行为与死地。(

2016-07-06 11:30:13 425

原创 Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路:题意给定一个n阶的楼梯,每次走一步或者两步,问到达顶部

2016-06-21 14:30:46 291

原创 House Robber II

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a ci

2016-06-17 14:33:21 243

原创 House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house

2016-06-17 10:58:15 201

原创 Binary Tree Paths

Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:1/ \2 3\5All root-to-leaf paths are:["1->2->5", "1->3"]思路:题目要求是打印出来一个二叉树的从根到叶子节点

2016-06-17 10:16:41 202

原创 Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes

2016-06-16 16:48:27 264

原创 Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee

2016-06-16 15:20:36 242

原创 Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.思路:给定一个二叉搜索树,要求程序返回的是整个BST的第k小的节点值对于一个BST来说,其中序遍历的结果是升序的。那么在中序遍历访问节点的顺序上当第k个访问的时候,实际上这个节点就是题目要求的节点值

2016-06-16 14:46:56 209

原创 Invert Binary Tree

Invert a binary tree.4/ \2 7/ \ / \1 3 6 9to4/ \7 2/ \ / \9 6 3 1思路:题目要求将一个二叉树的左右孩子进行交换。递归解决即可/** * Definition for a binary tree node. * struct TreeNode { * int va

2016-06-16 14:34:45 235

原创 Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes.In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as poss

2016-06-16 14:11:57 224

原创 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 /

2016-06-16 10:58:22 287

原创 Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()

2016-06-14 16:30:37 214

原创 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},1\2/3return [3,2,1].思路:求一棵二叉树的后续遍历的序列方法一:递归/** * Definition fo

2016-06-14 15:40:21 220

原创 Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3},1\2/3return [1,2,3].思路:题目要求一个二叉树的先序遍历结果方法一:递归方式/** * Definition

2016-06-14 15:24:06 278

原创 Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total

2016-06-14 15:04:46 180

原创 Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the total

2016-06-14 14:52:50 201

原创 Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant

2016-06-07 16:08:13 173

原创 Populating Next Right Pointers in Each Node

Given a binary treestruct TreeLinkNode {TreeLinkNode *left;TreeLinkNode *right;TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right n

2016-06-07 15:40:14 172

原创 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.For example,Given1/ \2 5/ \ \3 4 6The flattened tree should look like:1\2\3\4\5\6思路:题目要求将一个二叉树修改为一

2016-06-06 15:06:11 195

原创 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22,5/ \4 8/ / \11 13 4/ \ / \

2016-06-06 11:21:27 202

原创 Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and su

2016-06-06 10:54:08 200

原创 Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:给定一个二叉树,返回这个二叉树的最小深度。最小深度的定义是从roo

2016-06-04 16:34:40 183

原创 Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe

2016-06-04 16:19:20 189

原创 Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:题目要求根据提供的有序的array,将其转化为一个平衡的二叉搜索树。可以考虑如下解决方式:(1)找到数组中的中间元素,以中间元素为分割点,作为树的根,前半部分为左子树的节点值,右半部分为右

2016-06-04 16:04:11 185

原创 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},

2016-06-04 15:55:53 181

原创 Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路:返回一个二叉树的最大的高度。思考递归的解决方式:(1)如果

2016-06-04 11:31:23 212

原创 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary

2016-06-03 15:48:53 154

原创 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 7re

2016-06-03 15:28:16 139

原创 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric:1/ \2 2/ \ / \3 4 4 3But the following is not

2016-06-03 15:06:53 166

原创 Same Tree

Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.思路:给定两个二叉树,判

2016-06-03 14:41:09 173

原创 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.

2016-06-03 10:22:51 212

原创 Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below.1 3 3 2 1\

2016-06-02 16:02:10 172

原创 Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's.1 3 3 2 1\ / / / \ \3 2 1 1 3 2/

2016-06-02 14:13:55 268

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除