Skip to content

Commit

Permalink
1.在找公共父节点的算法中新增一种思路2.完成了“求树中两个节点的最短路径”算法
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteen committed Jan 30, 2018
1 parent 4e79c72 commit febfa8c
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 5 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 31 additions & 5 deletions 最近公共父节点/最近公共父节点/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct TreeNode {
}
};

// 找最近公共父节点
TreeNode* findClosestParent(TreeNode *A, TreeNode *B, TreeNode *root) {
// 找最近公共父节点(思路一)
TreeNode* findClosestParent1(TreeNode *A, TreeNode *B, TreeNode *root) {
if (root == NULL) {
return NULL;
}
Expand All @@ -29,8 +29,8 @@ TreeNode* findClosestParent(TreeNode *A, TreeNode *B, TreeNode *root) {
return root;
}

TreeNode *leftNode = findClosestParent(A, B, root->left);
TreeNode *rightNode = findClosestParent(A, B, root->right);
TreeNode *leftNode = findClosestParent1(A, B, root->left);
TreeNode *rightNode = findClosestParent1(A, B, root->right);

if (leftNode != NULL && rightNode != NULL) {
return root;
Expand All @@ -47,7 +47,33 @@ TreeNode* findClosestParent(TreeNode *A, TreeNode *B, TreeNode *root) {
return NULL;
}


// 找最近公共父节点(思路二)
TreeNode *findClosestParent2(TreeNode *A, TreeNode *B, TreeNode *root) {
if (root == NULL) {
return NULL;
}
TreeNode *currentLeft = findClosestParent1(A, B, root->left);
TreeNode *currentRight = findClosestParent1(A, B, root->right);
// 情况一:左右都为空
if (currentLeft == NULL && currentRight == NULL) {
// 1.1:root为目标节点
if (root == A || root == B) {
return root;
// 1.2:root不为目标节点
} else {
return NULL;
}
// 情况二:左边右边都不为空
} else if (currentLeft != NULL && currentRight != NULL) {
return root;
// 情况三:左边不为空,右边为空
} else if (currentLeft != NULL) {
return currentLeft;
// 情况四:左边为空,右边不为空
} else {
return currentRight;
}
}

int main(int argc, const char * argv[]) {
// insert code here...
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 48;
objects = {

/* Begin PBXBuildFile section */
6A49D541202061F400AB7E88 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A49D540202061F400AB7E88 /* main.cpp */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
6A49D53B202061F400AB7E88 /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = /usr/share/man/man1/;
dstSubfolderSpec = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 1;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
6A49D53D202061F400AB7E88 /* 求树中两个节点的最短路径 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "求树中两个节点的最短路径"; sourceTree = BUILT_PRODUCTS_DIR; };
6A49D540202061F400AB7E88 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
6A49D53A202061F400AB7E88 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
6A49D534202061F400AB7E88 = {
isa = PBXGroup;
children = (
6A49D53F202061F400AB7E88 /* 求树中两个节点的最短路径 */,
6A49D53E202061F400AB7E88 /* Products */,
);
sourceTree = "<group>";
};
6A49D53E202061F400AB7E88 /* Products */ = {
isa = PBXGroup;
children = (
6A49D53D202061F400AB7E88 /* 求树中两个节点的最短路径 */,
);
name = Products;
sourceTree = "<group>";
};
6A49D53F202061F400AB7E88 /* 求树中两个节点的最短路径 */ = {
isa = PBXGroup;
children = (
6A49D540202061F400AB7E88 /* main.cpp */,
);
path = "求树中两个节点的最短路径";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
6A49D53C202061F400AB7E88 /* 求树中两个节点的最短路径 */ = {
isa = PBXNativeTarget;
buildConfigurationList = 6A49D544202061F400AB7E88 /* Build configuration list for PBXNativeTarget "求树中两个节点的最短路径" */;
buildPhases = (
6A49D539202061F400AB7E88 /* Sources */,
6A49D53A202061F400AB7E88 /* Frameworks */,
6A49D53B202061F400AB7E88 /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = "求树中两个节点的最短路径";
productName = "求树中两个节点的最短路径";
productReference = 6A49D53D202061F400AB7E88 /* 求树中两个节点的最短路径 */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
6A49D535202061F400AB7E88 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0920;
ORGANIZATIONNAME = meituan;
TargetAttributes = {
6A49D53C202061F400AB7E88 = {
CreatedOnToolsVersion = 9.2;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = 6A49D538202061F400AB7E88 /* Build configuration list for PBXProject "求树中两个节点的最短路径" */;
compatibilityVersion = "Xcode 8.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 6A49D534202061F400AB7E88;
productRefGroup = 6A49D53E202061F400AB7E88 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
6A49D53C202061F400AB7E88 /* 求树中两个节点的最短路径 */,
);
};
/* End PBXProject section */

/* Begin PBXSourcesBuildPhase section */
6A49D539202061F400AB7E88 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6A49D541202061F400AB7E88 /* main.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin XCBuildConfiguration section */
6A49D542202061F400AB7E88 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
6A49D543202061F400AB7E88 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
};
name = Release;
};
6A49D545202061F400AB7E88 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
6A49D546202061F400AB7E88 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_STYLE = Automatic;
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
6A49D538202061F400AB7E88 /* Build configuration list for PBXProject "求树中两个节点的最短路径" */ = {
isa = XCConfigurationList;
buildConfigurations = (
6A49D542202061F400AB7E88 /* Debug */,
6A49D543202061F400AB7E88 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
6A49D544202061F400AB7E88 /* Build configuration list for PBXNativeTarget "求树中两个节点的最短路径" */ = {
isa = XCConfigurationList;
buildConfigurations = (
6A49D545202061F400AB7E88 /* Debug */,
6A49D546202061F400AB7E88 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 6A49D535202061F400AB7E88 /* Project object */;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>求树中两个节点的最短路径.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
Loading

0 comments on commit febfa8c

Please sign in to comment.