突然得知19号就是career fair, 吓得我精神抖擞。
一定要坚持!!
Leetcode 98 Validate Binary Search Tree
Description:
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume aBST is defined as follows:
The leftsubtree of a node contains only nodes with keys less than the node’s key.
The rightsubtree of a node contains only nodes with keys greater than the node’s key.
Both theleft and right subtrees must also be binary search trees.
Methods:
想法就是,按照中序遍历的顺序遍历这个binary tree, 然后check是否node的值是递增的(!注意,不是非减)
需要栈的辅助(非递归的中序遍历)
并且使while循环不断进行的条件是:
|
|
运行到整棵树的根节点和一开始的时候,会出现stack.isEmpty()==true的情况,但此时root!=null;
其他时候都是root会等于null,但是stack非空。
节点值比较小,所有用long作为tmp的type。
Code:
|
|
大佬,我是真的不会dfs。
Leetcode 4 Median of Two Sorted Arrays
Description:
Thereare two sorted arrays nums1 and nums2 of size m and n respectively.
Find the medianof the two sorted arrays. The overall run time complexity should be O(log(m+n)).
Methods:
这题,我是脑子真的转不过来。
先用最土的方法吧: Merge sort. Find the median.
Code:
|
|
改天回来再做!
Leetcode 7 Reverse Integer
Description:
Given a 32-bit signed integer, reverse digits of an integer.
Methods:
这题比较简单
但是有一个tricky的地方,就是如何判断是否overflow
可以在每次乘10的时候,比较乘10之前和乘10之后的除了个位之外的数是否相同。
如果不同,就是溢出了。
Code:
|
|
闲话:
网页快做来不及啦!!