@@ -533,4 +533,125 @@ + (BinarySortTree *)getMiddleSortNode:(BinarySortTree *)node {
533
533
return middleNode;
534
534
}
535
535
536
+ // 判断二叉树是否是二叉搜索树
537
+ + (BOOL )isBinarySearchTree : (BinarySortTree *)root {
538
+
539
+ if (nil == root) {
540
+ return YES ;
541
+ }
542
+
543
+ if ((root.leftNode && root.value < root.leftNode .value ) || (root.rightNode && root.value > root.rightNode .value )) {
544
+ return NO ;
545
+ }
546
+ [self isBinarySearchTree: root.leftNode];
547
+ [self isBinarySearchTree: root.rightNode];
548
+
549
+ return YES ;
550
+ }
551
+
552
+ // 判断一棵树是否是另一棵树的子树
553
+ + (BOOL )isContainTree : (BinarySortTree *)root childNode : (BinarySortTree *)childNode {
554
+ if (nil == childNode) {
555
+ return YES ;
556
+ }
557
+
558
+ if (nil == root) {
559
+ return NO ;
560
+ }
561
+
562
+ if (root.value == childNode.value ) {
563
+ if ([self isMatch: root childNode: childNode]) {
564
+ return YES ;
565
+ }
566
+ }
567
+
568
+ return [self isContainTree: root.leftNode childNode: childNode] || [self isContainTree: root.rightNode childNode: childNode];
569
+ }
570
+
571
+ + (BOOL )isMatch : (BinarySortTree *)root childNode : (BinarySortTree *)childNode {
572
+ if (nil == root && nil == childNode) {
573
+ return YES ;
574
+ }
575
+ if (nil == root || nil == childNode) {
576
+ return NO ;
577
+ }
578
+ if (root.value != childNode.value ) {
579
+ return NO ;
580
+ }
581
+ return [self isMatch: root.leftNode childNode: childNode.leftNode] && [self isMatch: root.rightNode childNode: childNode.rightNode];
582
+ }
583
+
584
+ // 找出一棵二叉树中所有路径,并且其路径之和等于一个给定的值
585
+ + (void )findAllPathInTree : (BinarySortTree *)root pathArray : (NSMutableArray *)path vectore : (NSMutableArray *)vectore sum : (NSInteger )sum {
586
+ if (nil == root) {
587
+ return ;
588
+ }
589
+ if (nil != root && nil == root.leftNode && nil == root.rightNode && sum - root.value > 0 ) {
590
+ [path removeLastObject ];
591
+ }
592
+
593
+ [path addObject: [NSNumber numberWithInteger: root.value]];
594
+ if (sum == root.value ) {
595
+ [vectore addObject: [[NSMutableArray alloc ] initWithArray: path]];
596
+ }
597
+
598
+ [self findAllPathInTree: root.leftNode pathArray: path vectore: vectore sum: (sum - root.value)];
599
+ [self findAllPathInTree: root.rightNode pathArray: path vectore: vectore sum: (sum - root.value)];
600
+ }
601
+
602
+ // 中序遍历不带父链接的二叉树,找到给定节点的下一个节点,后继为比当前节点大的所有节点中值最小的那个
603
+ + (BinarySortTree *)findSuccessor : (BinarySortTree *)root searchNode : (BinarySortTree *)node {
604
+ if (nil == root) {
605
+ return nil ;
606
+ }
607
+
608
+ if (nil != node.rightNode ) {
609
+ BinarySortTree *left = node.rightNode ;
610
+ while (left.leftNode ) {
611
+ left = left.leftNode ;
612
+ }
613
+ return left;
614
+ }
615
+
616
+ BinarySortTree *parentNode = nil ;
617
+ while (root) {
618
+ if (root.value > node.value ) {
619
+ parentNode = root;
620
+ root = root.leftNode ;
621
+ }
622
+ else {
623
+ root = root.rightNode ;
624
+ }
625
+ }
626
+
627
+ return parentNode;
628
+ }
629
+
630
+ // 中序遍历不带父链接的二叉树,找到给定节点的下一个节点,后继为比当前节点小的所有节点中值最大的那个
631
+ + (BinarySortTree *)findPrecursor : (BinarySortTree *)root searchNode : (BinarySortTree *)node {
632
+ if (nil == root) {
633
+ return nil ;
634
+ }
635
+
636
+ if (node.leftNode ) {
637
+ BinarySortTree *right = node.leftNode ;
638
+ while (right.rightNode ) {
639
+ right = right.rightNode ;
640
+ }
641
+ return right;
642
+ }
643
+
644
+ BinarySortTree *parentNode = nil ;
645
+ while (root) {
646
+ if (root.value >= node.value ) {
647
+ root = root.leftNode ;
648
+ }
649
+ else {
650
+ parentNode = root;
651
+ root = root.rightNode ;
652
+ }
653
+ }
654
+ return parentNode;
655
+ }
656
+
536
657
@end
0 commit comments